import store from '@/store/index.js'; //需要引入store

export const px2upx = (value) => {
	value = Number(value);
	if (!value) {
		return value
	}
	let ratio = uni.upx2px(100) / 100;
	return value / ratio
}

export const nav = (url, data) => {
	let allTabBarList = store.state.$tabBarList;
	if (!url) {
		return
	}
	if (!data) {
		data = {}
	}
	let tabPath = allTabBarList.map(v => v.pagePath)
	if (tabPath.includes(url)) {
		uni.$u.route({
			url: url,
			type: 'switchTab'
		})
	} else {
		// 带参数
		uni.$u.route(url, {
			...data
		});
	}
}

export const navLogin = (url, data) => {
	if (store.getters.get_isLogin) {
		nav(url, data);
	} else {
		uni.$u.toast('请先登录');
		// nav('/pages/login/login');
		uni.$u.route({
			url: 'pages/login/login',
			type:'reLaunch'
		});
	}
}
export const navRedirect = (url, data) => {
	let allTabBarList = store.state.$tabBarList;
	if (!url) {
		return
	}
	if (!data) {
		data = {}
	}
	let tabPath = allTabBarList.map(v => v.pagePath)
	if (tabPath.includes(url)) {
		uni.$u.route({
			url: url,
			type: 'switchTab'
		})
	} else {
		// 带参数
		uni.$u.route({
			url:url,
			params: {...data},
			type:'redirect'
		});
	}
}
export const navRedirectLogin = (url, data) => {
	if (store.getters.get_isLogin) {
		navRedirect(url, data);
	} else {
		uni.$u.toast('请先登录');
		// nav('/pages/login/login');
		uni.$u.route({
			url: 'pages/login/login',
			type:'reLaunch'
		});
	}
}


export const modal = (content) => {
	let params = {
		title: '提示',
		content: '',
		confirmColor: '#117B12',
		showCancel: true
	};
	if (typeof content == 'string') {
		params.content = content;
	}
	if (typeof content == 'object') {
		Object.assign(params, content);
	}

	return new Promise((resolve, reject) => {
		uni.showModal({
			...params,
			success: function(res) {
				if (res.confirm) {
					resolve();
				} else if (res.cancel) {
					reject()
				}
			}
		});
	})
}

export const toastNavBack = (title) => {
	uni.$u.toast(title);
	setTimeout(() => {
		uni.navigateBack();
	}, 1800)
}

export const uniRequestPayment = (data) => {
	return new Promise(function(resolve, reject) {
		uni.requestPayment({
		    provider: 'wxpay',
			timeStamp: data.timeStamp,
			nonceStr: data.nonceStr,
			package:  data.package,
			signType: data.signType,
			paySign: data.paySign,
			success: function (res) {
				console.log('success:' + JSON.stringify(res));
				uni.$u.toast('支付成功');
				resolve(res);
			},
			fail: function (err) {
				if(err.errMsg == 'requestPayment:fail cancel'){
					uni.$u.toast('您取消了支付');
				}else{
					uni.$u.toast(err.errMsg || '支付失败');
				}
				reject(err);
				// console.log('fail:' + JSON.stringify(err));
			}
		});
	});
}

// 隐藏手机中间4位数  格式：1820****111
export const hidePhone = (data) => {
    return data.replace(/^(\d{3})\d+(\d{4})$/, "$1****$2")
}

export const htmlAddImgUrl = (html,baseImgUrl) => {
	html = html.replace(/<img [^>]*src=['"]/g, `<img src="${baseImgUrl}`) // 往图片追加根域名和'/'
	html = html.replace(/<source [^>]*src=['"]/g, `<source src="${baseImgUrl}`)
	return html
}